<?php
class GuestbookApp extends MemberbaseApp
{
	var $_gb_mod;
	
	function __construct(){
		$this->GuestbookApp();
	}
	
	function GuestbookApp(){
		parent::__construct();
		$this->_gb_mod=&m('guestbook');
	}
	
	
	function index(){
		$user=$this->visitor->get();
		$page = $this->_get_page();
		$reply=isset($_GET['reply'])?$_GET['reply']:'';
		if (in_array($user['role'], array(USER_CLIENT,USER_FACTORY,USER_MERCHANT))){
			$condition="user_id=".$user['user_id'];
		}elseif($user['role']==USER_MANAGER){
			$condition="1=1";
		}else{
			$condition="user_id =0";
		}
		if ($reply){
			$condition.=" and reply_time=0";
		}
		$books_list=$this->_gb_mod->find(array(
			'conditions'=>$condition,
			'count'=>true,
			'limit'=>$page['limit'],
			'order'=>'post_time desc,reply_time desc'	
		));
		if($user['role']==USER_MANAGER){
			if (!empty($books_list)){
				$member_mod=&m('member');
				foreach ($books_list as $key=>$val){
					$user_id=$val['user_id'];
					$member_info=$member_mod->get($user_id);
					$books_list[$key]['role']=$member_info['role'];
					$books_list[$key]['user_name']=$member_info['user_name'];
				}
			}
		}
		$this->assign('user',$user);
		$this->assign('books_list',$books_list);
		$this->display('guestbook.index.html');
		$this->_curlocal(LANG::get("guestbook_list"));
	}
	
	function add(){
		$user=$this->visitor->get();
		if (in_array($user['role'], array(USER_CLIENT,USER_FACTORY,USER_MERCHANT))){
			if (!IS_POST){
				$books=array('if_show'=>1);
				$this->_curlocal(LANG::get('add_guestbook'));
				$this->assign('books',$books);
				$this->display('guestbook.form.html');
			}else{
				$data=array();
				$gbtitle=trim($_POST['gb_title']);
				$gbcontent=trim($_POST['guestbook_content']);
				if (!$gbtitle){
					$this->show_warning("gbtitle_required");
					return;
				}	
				if (!$gbcontent){
					$this->show_warning("guestcontent_required");
					return;
				}
				$data['gb_title']=$gbtitle;
				$data['guestbook_content']=$gbcontent;
				$data['user_id']=$user['user_id'];
				$data['if_show']=isset($_POST['if_show'])?$_POST['if_show']:0;
				$data['post_time']=gmtime();
				$this->_gb_mod->add($data);
				if ($this->_gb_mod->has_error()){
					$this->show_warning($this->_gb_mod->get_error());
					return;
				}
				$this->show_message('add_success', Lang::get('guestbook_list'), 'index.php?app=guestbook');
			}
		}else{
			$this->show_warning('no_powers_to_visit');
			return;
		}
	}
	
	function show(){
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$id) {
			$this->show_warning('books_not_exist');
			return;
		}
		$books = $this->_gb_mod->get(array(
			'conditions' => 'gb_id = ' . $id,
		));
		if (empty($books)) {
			$this->show_warning('books_not_exist');
			return;
		}
		
		$this->assign('books', $books);
		$this->_curlocal(LANG::get("guestbook_list"), 'index.php?app=guestbook',
				$books['gb_title']);
		$this->display('guestbook.show.html');
	}
	
	function reply(){
		$user=$this->visitor->get();
		if (in_array($user['role'], array(USER_MANAGER))){
			$gb_id = empty($_GET['gb_id']) ? 0 : intval($_GET['gb_id']);
			if (!$gb_id) {
				$this->show_warning('books_not_exist');
				return;
			}
			if (!IS_POST){
				$books=$this->_gb_mod->get_info($gb_id);
				$this->_curlocal(LANG::get('edit_guestbook'));
				$this->assign('books',$books);
				$this->display('guestbook.form.html');
			}else{
				$data=array();
				$replycontent=trim($_POST['reply_content']);
				if (!$replycontent){
					$this->show_warning("replycontent_required");
					return;
				}
				$data['reply_content']=$replycontent;
				$data['if_show']=isset($_POST['if_show'])?$_POST['if_show']:0;
				$data['reply_time']=gmtime();
				$this->_gb_mod->edit($gb_id,$data);
				if ($this->_gb_mod->has_error()){
					$this->show_warning($this->_gb_mod->get_error());
					return;
				}
				$this->show_message('reply_success', Lang::get('guestbook_list'), 'index.php?app=guestbook');
			}
		}else{
			$this->show_warning('no_powers_to_visit');
			return;
		}
	}
	
	
	function drop(){
		$gb_id = empty($_GET['gb_id']) ? 0 : intval($_GET['gb_id']);
		if (!$gb_id) {
			$this->show_warning('books_not_exist');
			return;
		}
		$books = $this->_gb_mod->get_info($gb_id);
		if(empty($books)) {
			$this->show_warning('books_not_exist');
			return;
		}
		$this->_gb_mod->drop("gb_id={$gb_id}");
		if ($this->_gb_mod->has_error()) {
			$this->_error($this->_gb_mod->get_error());
			return;
		}
		$this->show_message('删除留言成功', Lang::get('guestbook_list'), 'index.php?app=guestbook');
	}
}